﻿using System;

using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SmartDeviceProject1
{
    [Serializable]
    class Alarm
    {
        private static string saveFile =  @"\Application Data\ActiveAlarm.txt";

        private AlarmState state = AlarmState.off;
        private DateTime time;

        public DateTime Time
        {
            get 
            {
                if (state == AlarmState.on)
                    return time;
                else
                    throw new AlarmOffException();
            }
        }

        public string PlayFile
        {
            get { return this.id; }
        }

        public static bool Saved
        {
            get
            {
                if (File.Exists(saveFile)) return true; else return false;
            }
        }

        internal AlarmState State
        {
            get { return state; }
        }

        private string id;

        public Alarm(string id)
        {
            this.id = id;
            
        }


        public static Alarm Load()
        {
            if (File.Exists(saveFile))
            {
                //read from file
                StreamReader fileStream = new StreamReader(File.OpenRead(saveFile));
                string id = fileStream.ReadLine();
                DateTime time = DateTime.Parse(fileStream.ReadLine());
                fileStream.Close();

                //create object after parsing
                Alarm result = new Alarm(id);
                result.state = AlarmState.on;
                result.time = time;
                
                return result;
            }
            else return null;
        }

        private static void save(Alarm alarm)
        {
            StreamWriter filestream = new StreamWriter(File.OpenWrite(saveFile));
            filestream.WriteLine(alarm.id);
            filestream.WriteLine(alarm.time);
            filestream.Close();
        }

        public void On(DateTime time)
        {
            //if the datetime has passed, increment the day
            if (time.CompareTo(DateTime.Now) < 0)
            {
                time = time.AddDays(1);
            }

            this.time = time;
            long fileStartTime = time.ToFileTime();
            long localFileStartTime = 0;
            Win32.FileTimeToLocalFileTime(ref fileStartTime, ref localFileStartTime);
            SystemTime systemStartTime = new SystemTime();

            Win32.FileTimeToSystemTime(ref localFileStartTime, systemStartTime);
            Win32.CeRunAppAtTime(id, systemStartTime);

            state = AlarmState.on;

            save(this);
        }

        public void Off()
        {
            Win32.CeRunAppAtTime(id, null);
            state = AlarmState.off;

            File.Delete(saveFile);
        }

        public Alarm(DateTime when, string what):this(what)
        {
            this.On(when);
        }

   }

    enum AlarmState
    {
        on,
        off
    }
}
